#!/bin/bash
# Auto-generating dependencies for glibc development snapshots.
#
# A glibc development snapshot (say version 2.33.9000) may define
# symbols in its under-development symbol version (GLIBC_2.34).  RPM
# automatically derives RPM dependencies such as
# libc.so.6(GLIBC_2.34)(64bit) from that.  While the GLIBC_2.34
# version is under development, these dependencies may be inaccurate
# and could be satisfied by glibc RPM package versions that lack the
# symbols because they were created from an earlier development
# snapshot that had some other GLIBC_2.34 symbols.  Therefore, if the
# latest, under-development ELF symbol version is detected, this
# dependency generator adds an explicit RPM dependencies on the glibc
# packaging version against which an RPM package is built.
#
# This script runs for the glibc build itself.  In this case, it may
# produce a >= dependency on the build-time glibc, but there will also
# be an (potentially indirect) = dependency, which takes precedence.

set -e
set -o pipefail

searching=true
# Pre-filter using eu-elfclassify, to skip kernel modules.
eu-elfclassify --loadable --file --stdin --print | while read path; do
    # Assume that all dynamically linked objects depend on glibc in
    # some way.
    if $searching; then
	# Undefined symbols within latest, under-development
	# (changing) symbol versions trigger the versioned RPM
	# dependency.  Do not use "egrep -q" to keep reading from the
	# pipe, avoiding a spurious EPIPE error in eu-readelf.
	if eu-readelf -s "$path" \
		| egrep '\sUNDEF\s.*@''@SYMVER@(\s|$)' >/dev/null
	then
	    echo 'glibc >= @VERSION@-@RELEASE@'
	    # Stop searching after the first match, but keep reading from
	    # the pipe.
	    searching=false
	fi
    fi
done
